﻿using Moq;
using Sustainalytics.ClientDW.DataAccessLayer;
using Sustainalytics.DataLayer;
using Sustainalytics.Entities;
using Sustainalytics.Replication.Publisher;
using System;
using System.Collections.Generic;
using Xunit;

namespace Sustainalytics.ScreeningTool.BL.Tests
{
    public class FilterCriteriaManagerTests
    {
        private readonly Dictionary<string, string> serviceBusParams = new Dictionary<string, string>
        {
            { "ConnectionString", "Endpoint=sb://sust-test-ns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=Dh/4Cq0qrOC7xOJFUYHLGJvQ6pqj4BuwjTS4HUjL6uQ=" },
            { "RegionEndpoint", "TT" },
            { "SessionId", "Screening" },
            { "TopicPath", "test-topic" },
        };

        private readonly ServiceBusPublisher _serviceBusPublisher;

        public FilterCriteriaManagerTests()
        {
            _serviceBusPublisher = new ServiceBusPublisher(serviceBusParams);
        }

        #region Delete

        [Fact]
        public void DeleteFilterCriteriaById()
        {
            var filterCriteriaId = new Guid("5c686f08-e58f-4f98-9cdd-006b7d14fa20");

            var mockICompanyRepository = new Mock<ICompanyScreeningRepository>();
            var mockIClientDWRepoitory = new Mock<IClientDwRepository>();
            var mockIFilterCriteriaRepository = new Mock<IFilterCriteriaRepository>();

            mockIFilterCriteriaRepository.Setup(x => x.GetFilterCriteriaById(filterCriteriaId))
                .Returns((FilterCriteria)null);


            var filterCriteriaManager = FilterCriteriaManager.GetInstance(
                mockICompanyRepository.Object,
                mockIClientDWRepoitory.Object,
                mockIFilterCriteriaRepository.Object,
                _serviceBusPublisher);

            filterCriteriaManager.DeleteFilterCriteriaById(filterCriteriaId);

            var listFilterCriteria = filterCriteriaManager.GetFilterCriteriaGroupByFilterCriteriaId(filterCriteriaId);

            Assert.Equal(0, listFilterCriteria.Count);
        }

        [Fact]
        public void DeleteFilterCriteriaGroup()
        {
            var filterId = new Guid("2fa3f715-e262-49d8-82dd-a06c904945d2");
            var filterCriteriaId = new Guid("5c686f08-e58f-4f98-9cdd-006b7d14fa20");
            var filterGroupId = new Guid("efa59168-4e0c-4ec4-b695-cf8acf19fbed");

            var mockICompanyRepository = new Mock<ICompanyScreeningRepository>();
            var mockIClientDWRepoitory = new Mock<IClientDwRepository>();
            var mockIFilterCriteriaRepository = new Mock<IFilterCriteriaRepository>();

            mockIFilterCriteriaRepository.Setup(x => x.GetFilterCriteriaById(filterCriteriaId))
                .Returns((FilterCriteria)null);

            var filterCriteriaManager = FilterCriteriaManager.GetInstance(
                mockICompanyRepository.Object,
                mockIClientDWRepoitory.Object,
                mockIFilterCriteriaRepository.Object,
                _serviceBusPublisher);

            filterCriteriaManager.DeleteFilterCriteriaGroup(filterId, filterCriteriaId, filterGroupId);

            var listFilterCriteria = filterCriteriaManager.GetFilterCriteriaGroupByFilterCriteriaId(filterCriteriaId);

            Assert.Equal(0, listFilterCriteria.Count);
        }

        [Fact]
        public void DeleteFilterCriteriaGroupFilterGroupIdNull()
        {
            var filterId = new Guid("2fa3f715-e262-49d8-82dd-a06c904945d2");
            var filterCriteriaId = new Guid("5c686f08-e58f-4f98-9cdd-006b7d14fa20");
            Guid? filterGroupId = null;

            var mockICompanyRepository = new Mock<ICompanyScreeningRepository>();
            var mockIClientDWRepoitory = new Mock<IClientDwRepository>();
            var mockIFilterCriteriaRepository = new Mock<IFilterCriteriaRepository>();

            mockIFilterCriteriaRepository.Setup(x => x.GetFilterCriteriaById(filterCriteriaId))
                .Returns((FilterCriteria)null);

            var filterCriteriaManager = FilterCriteriaManager.GetInstance(
                mockICompanyRepository.Object,
                mockIClientDWRepoitory.Object,
                mockIFilterCriteriaRepository.Object,
                _serviceBusPublisher);

            filterCriteriaManager.DeleteFilterCriteriaGroup(filterId, filterCriteriaId, filterGroupId);

            var listFilterCriteria = filterCriteriaManager.GetFilterCriteriaGroupByFilterCriteriaId(filterCriteriaId);

            Assert.Equal(0, listFilterCriteria.Count);
        }

        [Fact]
        public void DeleteFilterCriteriaByIdTemplateIdAndFilterId()
        {
            var filterId = new Guid("2fa3f715-e262-49d8-82dd-a06c904945d2");
            var filterCriteriaId = new Guid("5c686f08-e58f-4f98-9cdd-006b7d14fa20");
            var templateCriteriaId = new Guid("c357e4f0-a840-4df2-fa4e-1099024a7680");

            var mockICompanyRepository = new Mock<ICompanyScreeningRepository>();
            var mockIClientDWRepoitory = new Mock<IClientDwRepository>();
            var mockIFilterCriteriaRepository = new Mock<IFilterCriteriaRepository>();

            mockIFilterCriteriaRepository.Setup(x => x.GetFilterCriteriaById(filterCriteriaId))
                .Returns((FilterCriteria)null);

            var filterCriteriaManager = FilterCriteriaManager.GetInstance(
                mockICompanyRepository.Object,
                mockIClientDWRepoitory.Object,
                mockIFilterCriteriaRepository.Object,
                _serviceBusPublisher);

            filterCriteriaManager.DeleteFilterCriteriaByIdTemplateIdAndFilterId(templateCriteriaId, filterId);

            var listFilterCriteria = filterCriteriaManager.GetFilterCriteriaGroupByFilterCriteriaId(filterCriteriaId);

            Assert.Equal(0, listFilterCriteria.Count);
        }

        [Fact]
        public void DeleteFilterCriteriaByFilterGroupId()
        {
            var filterId = new Guid("2fa3f715-e262-49d8-82dd-a06c904945d2");
            var filterCriteriaId = new Guid("5c686f08-e58f-4f98-9cdd-006b7d14fa20");
            var filterGroupId = new Guid("efa59168-4e0c-4ec4-b695-cf8acf19fbed");

            var mockICompanyRepository = new Mock<ICompanyScreeningRepository>();
            var mockIClientDWRepoitory = new Mock<IClientDwRepository>();
            var mockIFilterCriteriaRepository = new Mock<IFilterCriteriaRepository>();

            mockIFilterCriteriaRepository.Setup(x => x.GetFilterCriteriaById(filterCriteriaId))
                .Returns((FilterCriteria)null);

            var filterCriteriaManager = FilterCriteriaManager.GetInstance(
                mockICompanyRepository.Object,
                mockIClientDWRepoitory.Object,
                mockIFilterCriteriaRepository.Object,
                _serviceBusPublisher);

            filterCriteriaManager.DeleteFilterCriteriaByFilterGroupId(filterId, filterGroupId);

            var listFilterCriteria = filterCriteriaManager.GetFilterCriteriaGroupByFilterCriteriaId(filterCriteriaId);

            Assert.Equal(0, listFilterCriteria.Count);
        }

        #endregion
    }
}
